#!/usr/bin/python

import sys
import os
from utils import dict_read
from utils import dict_write
from utils import list_read
from utils import list_write
from utils import open_store
from utils import match_or
from utils import match_and

from httpmine import httpmine_callback_path_text
from httpmine import httpmine_config_filename
from httpmine import phrases_filename

from textmine import textmine_config
from textmine import textmine_path_phrases
from textmine import textmine_path_phrases_sorted
from textmine import textmine_path_phrases_unique
from textmine import textmine_path_phrases_counts
from textmine import textmine_path_words
from textmine import textmine_path_words_sorted
from textmine import textmine_path_words_unique
from textmine import textmine_path_words_counts

from textmine import textmine_config_filename
from textmine import phrases_sorted_filename
from textmine import phrases_unique_filename
from textmine import phrases_counts_filename
from textmine import words_filename
from textmine import words_sorted_filename
from textmine import words_unique_filename
from textmine import words_counts_filename

word_phrase_filename = 'word_phrase.txt'

def _bin_search(list, item, first, last):
    "return index of 'item' in 'list'. assumes list is sorted"
    # if last < 0:
    #    last = len(list)-1
    x = last - first
    if (x < 0):
        return -1
    if (x == 0):
        n = first
    else:
        n = first + (x+1)/2

    # print "item=",item, "list[",n,"]=",list[n],"first=",first,"last=",last

    # binary search: split, recurse subrange:
    if list[n] == item:
        return n
    elif item < list[n]:
        #print "<"
        return _bin_search(list,item,first,n-1)
    else:
        #print ">"
        return _bin_search(list,item,n+1,last)

def test_bin_search():
    n = [0,5,928,10982,1089280,129839803,1231209833]
    print _bin_search( n, 129839803, 0, len(n)-1)
    n = range(0,32134,81)
    x = _bin_search(  n, 21303, 0, len(n)-1)
    print n[x],x
    n = range(0,32134,82)
    x = _bin_search(  n, 21303, 0, len(n)-1)
    print n[x],x

def evaluate():
    path = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
    httpmine_config = dict_read(os.path.join(path, httpmine_config_filename))
    path_text = httpmine_config[httpmine_callback_path_text]
    textmine_config = dict_read(os.path.join(path, textmine_config_filename))
    path_phrases = textmine_config[textmine_path_phrases]
    path_phrases_sorted = textmine_config[textmine_path_phrases_sorted]
    path_phrases_unique = textmine_config[textmine_path_phrases_unique]
    path_phrases_counts = textmine_config[textmine_path_phrases_counts]
    path_words = textmine_config[textmine_path_words]
    path_words_sorted = textmine_config[textmine_path_words_sorted]
    path_words_unique = textmine_config[textmine_path_words_unique]
    path_words_counts = textmine_config[textmine_path_words_counts]
    path_word_phrase = os.path.join(path_text, word_phrase_filename)

    words_unique_file = open(path_words_unique, 'r')
    words_unique = []
    for word in words_unique_file.xreadlines():
        auxiliar = word[:-1]
        if len(auxiliar) > 0:
            words_unique.append(auxiliar)
    words_unique_file.close()

    words_unique.sort()
    i = 1
    while i < len(words_unique):
          if words_unique[i-1] > words_unique[i]:
             print 'error at %d' % i
          i += 1
    words_unique_last = len(words_unique) - 1
    phrases_unique_file = open(path_phrases_unique, 'r')
    word_phrase_indexes = open(path_word_phrase, 'w')

    phrase_index = 0
    for line in phrases_unique_file.xreadlines():
        if line != '\n':
            for word in line.split():
                word_index = _bin_search(words_unique, word, 0, words_unique_last)
                if word_index > -1:
                    word_phrase_indexes.write('%s  %s\n' % (repr(word_index).zfill(6), repr(phrase_index).zfill(6))) 
                else:
                    word_phrase_indexes.write('%s  %s\n' % (repr(word).zfill(6), repr(phrase_index).zfill(6))) 
        phrase_index += 1
    word_phrase_indexes.close()
    phrases_unique_file.close()
    
if __name__ == '__main__':
   evaluate()

